<?php
namespace Admin\Controller;

class IndexController extends AuthController {
    public function index(){
        //dump(session());
       $this->display('index');
    }
    //轮播模板赋值
    public function adv(){
        $User=M('banner')->select();
        $this->assign('list',$User);
        $this->display('adv');
    }
    /*
     * ///添加首页轮播
     */
    public function add(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Public/'; // 设置附件上传根目录
        $upload->savePath  =      '/Uploads/'; // 设置附件上传（子）目录
        $info   =   $upload->upload();
        $array = array();
        if ($info) {
            foreach ($info as $val) {
                $array[] = $val['savepath'] . $val['savename'];
            }
        }
        $post = I('post.');
        $post['image']=$array[0];
       // dump($post);
        if(!$info) {// 上传错误提示错误信息

            //$this->error($upload->getError());
        }else{// 上传成功 获取上传文件信息
            foreach($info as $file){
                $banner=D('banner');
               if($banner->add($post)) {
                   //dump($post);
                  $this->success('上传成功','adv');
               }
            }
        }
        $this->display('add');
    }
    /*
     * ///修改轮播
     */
    public function edit($id){
        //原图片版赋值
        $banner=M('banner');
        $bannerfind=$banner->find($id);
        //dump($bannerfind['image']);
        $array['title']    =   $bannerfind['title'];
        $array['image']   =   $bannerfind['image'];
        //dump($array['image']);

        if(IS_POST){

            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =      './Public/'; // 设置附件上传根目录
            $upload->savePath  =      '/Uploads/'; // 设置附件上传（子）目录
            $info   =   $upload->upload();
            $array = array();
            //原图片路径
            $imgfile = ROOT_PATH . './public' .$bannerfind['image'];

            if ($info){
                //删除原图片
                if(is_file($imgfile)){
                    unlink($imgfile);
                }
                //新图片路径
                foreach ($info as $val) {
                    $array[] = $val['savepath'] . $val['savename'];
                }
                //dump($array[0]);
                }
                $post = I('post.');
                $post['image']=$array[0];
               dump($post);

            if($banner->create() && !empty($post['image'])){
                if($banner->save($post)){
                    $this->success('编辑成功',U('index/adv'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($banner->getError());
            }
        }
        //传id过去
        $this->assign($array);
        $this->assign('bans' ,$bannerfind);
        $this->display();
    }
    /*
     * ///删除轮播
     */
    public  function del($id){
        $banner= M('banner');
        $bans = $banner->find($id);
        $imgfile = ROOT_PATH . './public' .$bans['image'];
        if(is_file($imgfile)){
            unlink($imgfile);
        }

        if($banner->delete($id)){
            redirect( U('index/adv'), 0, '页面跳转中...');
        }else{
            $this->error('删除失败');
        }
    }
    /*
     ////////////////////////////////////////留言管理
     */
    public function book(){
        $User       = M('about'); // 实例化User对象
        $count      = $User->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,3);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $User->order('about_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }
    public function about_del($id){
        $about= M('about');
        $abo = $about->find($id);
        $imgfile = ROOT_PATH . './public' .$abo['about_img'];
        if(is_file($imgfile)){
            unlink($imgfile);
        }
        if($about->delete($id)){
            redirect( U('index/book'), 0, '页面跳转中...');
           // $this->success('删除成功！' , U('index/book'));
        }else{
            $this->error('删除失败');
        }
    }
    /*
     * 留言删除
     */
    public function about_dels(){
        $about=M('about');
       $id=implode(',',($_POST['about_id']));
        if($about->delete($id)){
            redirect( U('index/book'), 0, '页面跳转中...');
        }else {
            echo "<script>alert('请勾选需要删除的内容'),{javascript:history.back(-1)}</script>";

        }
    }

    public function column(){
		$nav = M('nav');
		$navs = $nav->select();
        if(IS_POST){
            $post = I('post.');
            //dump($post);
			if($nav->create()){
                if($nav->add($post)){
                    $this->success('添加成功',U('index/column'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($nav->getError());
            }
        }
		$this->assign('navs' , $navs);
        $this->display('column');
    }
	
	public function editcolumn($id){
		$nav = M('nav');
		if(IS_POST){
            $post = I('post.');
            //dump($post);
			if($nav->create()){
                if($nav->save($post)){
                    $this->success('编辑成功',U('index/column'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($nav->getError());
            }
        }
		$navs = $nav->find($id);
		$this->assign('navs' , $navs);
		$this->display('editcolumn');
	}
	
	public function delcolumn($id){
		$nav = M('nav');
		$re = $nav->delete($id);
        if($re){
            $this->success('删除成功！' , U('index/column'));
        }else{
            $this->error('删除失败');
        }
	}

    public function page(){
        $this->display('page');
    }

}